//
//  HerosModel.m
//  项目二
//
//  Created by rimi on 16/5/24.
//  Copyright © 2016年 objective. All rights reserved.
//

#import "HerosModel.h"

@implementation HerosModel
//+ (instancetype)heroModelWithDictionary:(NSDictionary *)dic{
//    return [[self alloc]initWithHerosDictionary:dic];
//}
//
//- (instancetype)initWithHerosDictionary:(NSDictionary *)dic{
//    self = [super init];
//    if (self) {
//        /**使用KVC进行赋值*/
//        [self setValuesForKeysWithDictionary:dic];
//    }
//    return self;
//}
//
///**快速返回已经转换为英雄对象的数组*/
//+(NSArray *)heros{
//   /**获得文件的路径（转换的plist文件）*/
//    NSString *filrPath = [[NSBundle mainBundle]pathForResource:@"heros.plist" ofType:nil];
//    /**从文件中获得数组*/
//    NSArray *dicArray = [NSArray arrayWithContentsOfFile:filrPath];
//    NSLog(@"%@",dicArray);
//    /**将数组的字典循环取出，并转换为模型对象*/
//    NSMutableArray *heroModelArray = [NSMutableArray arrayWithCapacity:dicArray.count];
//    for (NSInteger index = 0; index < dicArray.count; index ++) {
//        HerosModel *hero = [HerosModel heroModelWithDictionary:dicArray[index]];
//        /**保存到模型数组*/
//        [heroModelArray addObject:hero];
//    }
//    return heroModelArray;
//}


+ (instancetype)herosModelWithDictionary:(NSDictionary *)dictionary {
    return [[self alloc]initWithHerosDictionary:dictionary];
}

- (instancetype)initWithHerosDictionary:(NSDictionary *)dictionary
{
    self = [super init];
    if (self) {
        
        // 使用KVC进行赋值
        [self setValuesForKeysWithDictionary:dictionary];
    }
    return self;
}


// 快速返回已经转换为英雄对象的数组
+ (NSArray *)heros {
    
    // 获得文件的路径(需要转换的plist文件)
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"heros.plist" ofType:nil];
    
    // 从文件中获得数组
    NSArray *dictArray = [NSArray arrayWithContentsOfFile:filePath];
    
    // 将数组里的字典循环取出，并转换为模型对象
    NSMutableArray *heroModelArray = [NSMutableArray arrayWithCapacity:dictArray.count];
    for (NSInteger index = 0; index < dictArray.count; index++) {
        HerosModel *hero = [HerosModel herosModelWithDictionary:dictArray[index]];
        
        // 保存到模型数组
        [heroModelArray addObject:hero];
    }
    return heroModelArray;
}
@end
